import {Entity} from "../../entity";
import {Alias, Cascade, Children, Column, Editor, Enum, Relate, Serialize} from "../../decorator";
import {IRelate} from "../../interface";
import {Property} from "../../util/element/property";
import {ProductCatalog, ProductCategory} from "../element/category";
import {Attachment} from "../../util/entity/attachment";

export class Product extends Entity implements IRelate{

    @Editor("String")
    @Alias("配件名称")
    @Serialize("name")
    public name: string = "";

    @Editor("String")
    @Alias("配件编码")
    @Serialize("code")
    public code: string = "";

    @Editor("Memo")
    @Alias("配件描述")
    @Serialize("description")
    public description: string = "";

    @Editor("String")
    @Alias("车型")
    @Serialize("model")
    public model: string = "";

    @Editor("String")
    @Alias("货架号")
    @Serialize("shelf")
    public shelf: string = "";

    @Editor("List")
    @Relate(ProductCatalog, "ProductCatalog")
    @Alias("一级分类")
    public catalog: ProductCatalog = null;
    @Serialize("catalog")
    public catalog_code: string = "";

    @Editor("List")
    @Cascade("catalog", "children")
    @Alias("二级分类")
    public category: ProductCategory = null;
    @Serialize("category")
    public category_code: string = "";

    @Editor("Number")
    @Alias("市场价")
    @Serialize("price")
    public price: number = 0;

    @Editor("Number")
    @Alias("优惠价")
    @Serialize("price1")
    public price1: number = 0;

    @Editor("Number")
    @Alias("VIP价")
    @Serialize("price2")
    public price2: number = 0;

    @Editor("Number")
    @Alias("底价")
    @Serialize("price3")
    public price3: number = 0;

    @Editor("Number")
    @Alias("成本价")
    @Serialize("cost")
    public cost: number = 0;

    @Editor("Number")
    @Alias("库存")
    @Serialize("amount")
    public amount: number = 0;

    @Serialize()
    public properties: any = {};

    @Editor("Property")
    @Alias("详细参数")
    public params: Array<Property> = [];

    @Editor("ImageList")
    @Children(Attachment)
    @Alias("图片列表")
    @Serialize()
    public pictures: Array<Attachment> = [];

    related: boolean;

    constructor() {
        super();
    }

    relate(config): void {
        this.catalog = config.catalogs.find( item => item.code == this.catalog_code);
        this.category = this.catalog && this.catalog.children.find( item => item.code == this.category_code);
        this.params = [];
        if(this.category && this.category.properties && this.category.properties.length > 0){
            this.category.properties.map(property => {
                const clone: any = property.clone();
                clone.value = this.properties ? this.properties[property.name] : null;
                this.params.push(clone);
            });
        }
        this.related = true;
    }

    fromJSON(obj) {
        super.fromJSON(obj);
        this.properties = this.properties || {};
    }

    toJSON(): any {
        this.catalog_code = this.related ? (this.catalog ? this.catalog.code : undefined) : this.catalog_code;
        this.category_code = this.related ? (this.category ? this.category.code : undefined) : this.category_code;
        this.properties = {};
        this.params.forEach( item => {
            this.properties[item.name] = item.value;
        });
        return super.toJSON();
    }

    create() {
        this.related = true;
        super.create();
    }

    clone(): Entity {
        const entity: any = super.clone();
        entity.params = this.params.map(item => item.clone());
        return entity;
    }

    copy(entity: any) {
        super.copy(entity);
        this.params = entity.params.map(item => item.clone());
    }

}


